﻿namespace BM.Tools.WebControls.MForm.Controls.Helper
{
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    /// <summary>
    /// A client-side link button
    /// </summary>
    public class Button : WebControl, ITextControl
    {
        /// <summary>
        /// Gets or sets OnClientClick.
        /// </summary>
        /// <value>
        /// The on client click.
        /// </value>
        public virtual string OnClientClick
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets Text.
        /// </summary>
        /// <value>
        /// The text inside the button.
        /// </value>
        public virtual string Text
        {
            get;
            set;
        }

        /// <summary>
        /// Gets TagKey.
        /// </summary>
        /// <value>
        /// The tag key of the button - <see cref="HtmlTextWriterTag.Div" />.
        /// </value>
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        /// <summary>
        /// Adds id attribute
        /// </summary>
        /// <param name="writer">
        /// The writer.
        /// </param>         
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);            
        }

        /// <summary>
        /// render the attributes of the anchor
        /// </summary>
        /// <param name="writer">
        /// The writer.
        /// </param>
        protected virtual void RenderAnchorAttributes(HtmlTextWriter writer)
        {
            writer.AddAttribute("href", "#");
            if (!String.IsNullOrEmpty(OnClientClick))
            {
                writer.AddAttribute("onclick", OnClientClick);
            }
        }

        /// <summary>
        /// renders the anchor inside the div
        /// </summary>
        /// <param name="writer">
        /// The writer.
        /// </param>
        protected override void RenderContents(HtmlTextWriter writer)
        {
            RenderAnchorAttributes(writer);
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write(Text);
            writer.RenderEndTag();
        }
    }
}